package ticketExercise;

/**
 * @author 王艺锡
 * @version 1.0
 */
//使用多线程，模拟三个窗口售票
public class SellTicket {
    public static void main(String[] args) {
        ST01 st01 = new ST01();
        ST01 st02 = new ST01();
        ST01 st03 = new ST01();
        //会出现超卖现象
        st01.start();
        st02.start();
        st03.start();

//        ST02 st02 = new ST02();
//        new Thread(st02).start();
//        new Thread(st02).start();
//        new Thread(st02).start();
    }
}
class ST01 extends Thread{
    public static int ticketcount = 100;

    @Override
    public void run() {
        while (ticketcount > 0){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("窗口" + Thread.currentThread().getName() +
                    "售出一张票,剩余票数:" + --ticketcount);


        }
    }
}

class ST02 implements Runnable{
    public static int ticketcount = 100;
    @Override
    public void run() {
    while (true){
        if(ticketcount <= 0){
            System.out.println("售票结束...");
            break;
        }

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("窗口" + Thread.currentThread().getName() +
                "售出一张票,剩余票数:" + --ticketcount);
    }
    }
}
